﻿namespace Sustainalytics.Portfolios.Service.Infrastructure
{
    using System.Net.Http;
    using System.Web.Http.Filters;

    using Sustainalytics.Portfolios.Service.Utility;

    public class CustomExceptionFilter : ExceptionFilterAttribute 
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            var exception = actionExecutedContext.Exception;
            var httpMessage = ErrorMessageFactory.Create(exception.GetType());

            if (httpMessage != null)
            {
                actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(httpMessage.Item1, httpMessage.Item2);
            }
        }
    }
}
